#include "StdAfx.h"
#include "Utility.h"
#include "CustomPicture.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomPicture, CStatic)

CCustomPicture::CCustomPicture(void)
{
	//String and Font Parameter
	m_nFontSizeNormal = 9;
	m_pFontSizeHilight = 13;

	m_pFontFamily = NULL;
	m_strTextFormat = NULL;
	m_enFontUnit = UnitPoint;
	
	m_nFontStyleNormal = FontStyleBold;
	m_nFontStyleHilight = FontStyleBold;
	
	m_pBrushNormal = m_pBrushHilight = NULL;
	m_pFontNormal = m_pFontHilight = NULL;

	//Image and Handle Parameter
	m_pImg = NULL;
	m_hBgDC = m_hDrawDC = NULL;
	m_hPParentWnd = m_hParentWnd = NULL;
	
}

CCustomPicture::~CCustomPicture(void)
{
	if(m_pFontNormal != NULL){ delete m_pFontNormal;  m_pFontNormal = NULL; }
	if(m_pFontHilight != NULL){ delete m_pFontHilight;  m_pFontHilight = NULL; }
	if(m_pBrushNormal != NULL){ delete m_pBrushNormal;  m_pBrushNormal = NULL; }
	if(m_pBrushHilight != NULL){ delete m_pBrushHilight;  m_pBrushHilight = NULL; }
	if(m_pFontFamily != NULL){ delete m_pFontFamily;  m_pFontFamily = NULL; }
	if(m_strTextFormat != NULL){ delete m_strTextFormat;  m_strTextFormat = NULL; }	

	if ( m_pImg )
			delete m_pImg;
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomPicture::Initialize(HWND hPParentWnd, HWND hParentWnd, HDC hBgDC, HDC hDrawDC, int nParentx, int nParenty)
{
	m_hPParentWnd = hPParentWnd;
	m_hParentWnd = hParentWnd;

	m_hBgDC = hBgDC;
	m_hDrawDC = hDrawDC;

	::GetWindowRect(m_hPParentWnd, m_rtPPRect);
	::GetWindowRect(m_hParentWnd, m_rtPRect);

	GetWindowRect(&m_rtRect);
	m_rtRect.OffsetRect(+nParentx, +nParenty);
	m_rtRect.OffsetRect(-m_rtPRect.left, -m_rtPRect.top);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Image Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomPicture::SetImage(UINT nImgID)
{
	if(m_pImg){ delete m_pImg;  m_pImg = NULL; }
	CUtility::ImageFromIDResource(nImgID, _T("PNG"), m_pImg);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//String Font Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomPicture::CreateFont()
{
	//----------------------new FontFamily
	if(m_pFontFamily == NULL)
		m_pFontFamily = new FontFamily(L"Arial");
	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus())
	{
		if (this->m_hWnd)
		{
			CFont *pCurFont = this->GetFont();  LOGFONT LgFong;  pCurFont->GetLogFont(&LgFong);
			if(m_pFontFamily){ delete m_pFontFamily;  m_pFontFamily = NULL;  m_pFontFamily = new FontFamily(LgFong.lfFaceName); }
		}
	}
	
	//----------------------new StringFormat
	if(m_strTextFormat == NULL)
	{
		m_strTextFormat = new StringFormat;
		m_strTextFormat->SetAlignment(StringAlignmentNear);
		m_strTextFormat->SetLineAlignment(StringAlignmentCenter);
		m_strTextFormat->SetTrimming(StringTrimmingEllipsisCharacter);
	}
	
	//----------------------new Font
	if(m_pFontNormal == NULL)
		m_pFontNormal = new Gdiplus::Font(m_pFontFamily,(Gdiplus::REAL)m_nFontSizeNormal, m_nFontStyleNormal, m_enFontUnit);
	if(m_pFontHilight == NULL)
		m_pFontHilight = new Gdiplus::Font(m_pFontFamily,(Gdiplus::REAL)m_pFontSizeHilight, m_nFontStyleHilight, m_enFontUnit);


	//----------------------new FontBrush
	if (m_pBrushNormal == NULL)
		m_pBrushNormal = new SolidBrush(Color(247, 45, 45, 45));	
	if (m_pBrushHilight == NULL)
		m_pBrushHilight = new SolidBrush(Color(247, 247, 247, 247));
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomPicture::DrawUI()
{
	BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);
	Graphics graphics( m_hDrawDC );

	graphics.DrawImage(m_pImg, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height());
	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}

void CCustomPicture::DrawText(CString strTitle1, CString strTitle2, CString strTitle3, int nTitleHilight)
{
	int nInterval = m_rtRect.Width() / 3;
	CRect rect1(m_rtRect.left + 25,                  m_rtRect.top + 5,    m_rtRect.left + nInterval,       m_rtRect.bottom);
	CRect rect2(m_rtRect.left + nInterval + 25,      m_rtRect.top + 5,    m_rtRect.left + 2 * nInterval,   m_rtRect.bottom);
	CRect rect3(m_rtRect.left + 2 * nInterval + 25,  m_rtRect.top + 5,    m_rtRect.right - 10,             m_rtRect.bottom);

	CreateFont();
	Graphics graphics( m_hDrawDC );
	if(nTitleHilight == 1)
	{
    	graphics.DrawString(strTitle1.GetBuffer(), -1, m_pFontHilight, 
			RectF((Gdiplus::REAL)rect1.left, (Gdiplus::REAL)rect1.top, (Gdiplus::REAL)rect1.Width(), (Gdiplus::REAL)rect1.Height()),
			m_strTextFormat, m_pBrushHilight);
		graphics.DrawString(strTitle2.GetBuffer(), -1, m_pFontNormal, 
			RectF((Gdiplus::REAL)rect2.left, (Gdiplus::REAL)rect2.top, (Gdiplus::REAL)rect2.Width(), (Gdiplus::REAL)rect2.Height()),
			m_strTextFormat, m_pBrushNormal);
		graphics.DrawString(strTitle3.GetBuffer(), -1, m_pFontNormal, 
			RectF((Gdiplus::REAL)rect3.left, (Gdiplus::REAL)rect3.top, (Gdiplus::REAL)rect3.Width(), (Gdiplus::REAL)rect3.Height()),
			m_strTextFormat, m_pBrushNormal);
	}
	else if(nTitleHilight == 2)
	{
		graphics.DrawString(strTitle1.GetBuffer(), -1, m_pFontNormal, 
			RectF((Gdiplus::REAL)rect1.left, (Gdiplus::REAL)rect1.top, (Gdiplus::REAL)rect1.Width(), (Gdiplus::REAL)rect1.Height()),
			m_strTextFormat, m_pBrushNormal);
		graphics.DrawString(strTitle2.GetBuffer(), -1, m_pFontHilight, 
			RectF((Gdiplus::REAL)rect2.left, (Gdiplus::REAL)rect2.top, (Gdiplus::REAL)rect2.Width(), (Gdiplus::REAL)rect2.Height()),
			m_strTextFormat, m_pBrushHilight);
		graphics.DrawString(strTitle3.GetBuffer(), -1, m_pFontNormal, 
			RectF((Gdiplus::REAL)rect3.left, (Gdiplus::REAL)rect3.top, (Gdiplus::REAL)rect3.Width(), (Gdiplus::REAL)rect3.Height()),
			m_strTextFormat, m_pBrushNormal);
	}
	else if(nTitleHilight == 3)
	{
		graphics.DrawString(strTitle1.GetBuffer(), -1, m_pFontNormal, 
			RectF((Gdiplus::REAL)rect1.left, (Gdiplus::REAL)rect1.top, (Gdiplus::REAL)rect1.Width(), (Gdiplus::REAL)rect1.Height()),
			m_strTextFormat, m_pBrushNormal);
		graphics.DrawString(strTitle2.GetBuffer(), -1, m_pFontNormal, 
			RectF((Gdiplus::REAL)rect2.left, (Gdiplus::REAL)rect2.top, (Gdiplus::REAL)rect2.Width(), (Gdiplus::REAL)rect2.Height()),
			m_strTextFormat, m_pBrushNormal);
		graphics.DrawString(strTitle3.GetBuffer(), -1, m_pFontHilight, 
			RectF((Gdiplus::REAL)rect3.left, (Gdiplus::REAL)rect3.top, (Gdiplus::REAL)rect3.Width(), (Gdiplus::REAL)rect3.Height()),
			m_strTextFormat, m_pBrushHilight);
	}
	strTitle1.ReleaseBuffer();
	strTitle2.ReleaseBuffer();
	strTitle3.ReleaseBuffer();
	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}

void CCustomPicture::DrawItem(LPDRAWITEMSTRUCT di)
{
	DrawUI();
}

BOOL CCustomPicture::ShowWindow(int nCmdShow)
{
	switch(nCmdShow)
	{
	case SW_SHOW:
		Invalidate();
		break;
	case SW_HIDE:
		BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);
		CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
		break;
	default:
		break;
	}

	return CStatic::ShowWindow(nCmdShow);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomPicture, CStatic)
	ON_WM_DRAWITEM()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
void CCustomPicture::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	ModifyStyle(0, BS_OWNERDRAW);
	CStatic::PreSubclassWindow();
}

BOOL CCustomPicture::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}